package com.airnews.ticket.ui;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.airnews.ticket.R;
import com.airnews.ticket.util.Constants;
import com.airnews.ticket.util.NetUtils;

/**
 * 欢迎页面
 */
public class Welcome extends BaseActivity {
	
	private Timer progressTimer = new Timer();
	private LinearLayout progressBar;
	private boolean isCancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.welcome);
		super.onCreate(savedInstanceState);
		
		if(!NetUtils.httpTest(this)){
			return;
		}
		
		/*DataCache.currPhone.imsi = PhoneUtils.getImsi(this);
		DataCache.currPhone.esn = PhoneUtils.getEsn(this);*/
		
		progressBar = (LinearLayout)findViewById(R.id.progressBar);
		
		//显示进度条
		showProgress();
		
		new Thread(){

			@Override
			public void run() {
				
				SystemClock.sleep(2000);
				//getRemoteInfo("1331609");
				
				if(!isCancel){
					Welcome.this.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Intent it = new Intent(Welcome.this, Main.class);
							startActivity(it);
							//overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
							finish();
						}
					});
				}
				
				//停止定时器
				progressTimer.cancel();
				progressTimer = null;
				
				//初始化目录
				initAppDirs();
			}
			
		}.start();
	}


	/**
	 * 显示进度条
	 */
	private void showProgress() {
		progressTimer.schedule(new TimerTask() {
			int count = progressBar.getChildCount();
			int[] drawables = {R.drawable.point_s, R.drawable.point};
			int i = 0;
			@Override
			public void run() {
				Welcome.this.runOnUiThread(new Runnable() {
					public void run() {
						if(i == count * drawables.length){
							i = 0;
						}
						ImageView img = (ImageView)progressBar.getChildAt(i%count);
						img.setImageResource(drawables[i/count]);
						i++;
					}
				});
			}
		}, 100, 500);
	}

	
	/**
	 * 初始化掌厅在SD卡上的目录
	 */
	private void initAppDirs() {
		if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
			File rootDir = new File(Environment.getExternalStorageDirectory(), Constants.DOWNLOAD_DIR);
			if(!rootDir.exists()){
				rootDir.mkdir();
			}
			File subDir = new File(rootDir, Constants.SUB_IMG_DIR);
			if(!subDir.exists()){
				subDir.mkdir();
			}
		}
	}


	@Override
	public void onBackPressed() {
		isCancel = true;
		finish();
	}
	
	
	/** 
     * 手机号段归属地查询 
     *  
     * @param phoneSec 手机号段 
     */  
    private void getRemoteInfo(String phoneSec) {  
    	 Log.i("OOOOOOOOOOO", "-----------------");
        // 命名空间  
        String nameSpace = "http://WebXml.com.cn/";  
        // 调用的方法名称  
        String methodName = "getMobileCodeInfo";  
        // EndPoint  
        String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
        // SOAP Action  
        String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";  
  
        // 指定WebService的命名空间和调用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);  
  
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
        rpc.addProperty("mobileCode", phoneSec);  
        rpc.addProperty("userId", "");  
  
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  
        envelope.bodyOut = rpc;  
        // 设置是否调用的是dotNet开发的WebService  
        envelope.dotNet = true;  
        // 等价于envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);  
  
        HttpTransportSE transport = new HttpTransportSE(endPoint);  
  
        try {  
            // 调用WebService  
            transport.call(soapAction, envelope);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
        // 获取返回的数据  
        SoapObject object = (SoapObject) envelope.bodyIn;  
  
        // 获取返回的结果  
        String result = object.getProperty("getMobileCodeInfoResult").toString();  
  
        // 将WebService返回的结果显示在TextView中  
        //resultView.setText(result);
        Log.i("OOOOOOOOOOO", result);
    }

}
